package com.hxd.otc.module.wallet.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.base.BaseFragment;
import com.hxd.otc.module.wallet.fragment.OtcAssestFragment;
import com.hxd.otc.mvp.base.BasePresenter;

import java.util.ArrayList;

import butterknife.BindView;


public class WalletActivity extends BaseActivity {

    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;

    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    String[] tabNames;

    OtcAssestFragment otcFragment;
    OtcAssestFragment normalFragment;
    private ArrayList<BaseFragment> mFragmentList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void initView() {
        initViewPager();

    }

    private void initViewPager() {
        tabNames = getResources().getStringArray(R.array.wallet_tab);
        addFragments();

        mViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabNames[position];
            }
        });

        mViewPager.setOffscreenPageLimit(mFragmentList.size());
        mViewPager.setCurrentItem(0, false);
        mTabLayout.setupWithViewPager(mViewPager, true);
        setupTabIcons();

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tabText = tab.getCustomView().findViewById(R.id.tv_bottom);
                tabText.setTextColor(getResources().getColor(R.color.color_text1));
                tabText.setTextSize(16);
                ImageView ivBotton = tab.getCustomView().findViewById(R.id.iv_botton);
                ivBotton.setVisibility(View.VISIBLE);
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tabText = tab.getCustomView().findViewById(R.id.tv_bottom);
                tabText.setTextColor(getResources().getColor(R.color.color_FF95A2AC));
                tabText.setTextSize(13);
                ImageView ivBotton = tab.getCustomView().findViewById(R.id.iv_botton);
                ivBotton.setVisibility(View.GONE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                switch (position) {
                    case 0:
                        otcFragment.refreshData();
                        normalFragment.clearSerchContent();
                        break;
                    case 1:
//                        normalFragment.refreshData();
//                        otcFragment.clearSerchContent();
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void setupTabIcons() {
        for (int i = 0; i < mFragmentList.size(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);//获得每一个tab
            tab.setCustomView(getTabView(i));//给每一个tab设置view
        }
    }

    public View getTabView(int position) {

        View view = LayoutInflater.from(this).inflate(R.layout.item_order_tab, null);
        TextView tv_bottom = view.findViewById(R.id.tv_bottom);
        tv_bottom.setText(tabNames[position]);
        tv_bottom.setTextColor(position == 0 ? getResources().getColor(R.color.color_text1) : getResources().getColor(R.color.color_FF95A2AC));
        ImageView ivBotton = view.findViewById(R.id.iv_botton);
        if (position == 0) {
            tv_bottom.setTextSize(16);
            ivBotton.setVisibility(View.VISIBLE);
        } else {
            tv_bottom.setTextSize(13);
            ivBotton.setVisibility(View.GONE);
        }
        return view;

    }

    private void addFragments() {
        mFragmentList = new ArrayList<>();
        otcFragment = new OtcAssestFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("pos", 0);
        otcFragment.setArguments(bundle);
        mFragmentList.add(otcFragment);

        normalFragment = new OtcAssestFragment();
        Bundle noBundle = new Bundle();
        noBundle.putInt("pos", 1);
        normalFragment.setArguments(noBundle);
        mFragmentList.add(normalFragment);
    }

    public OtcAssestFragment getNormalFragment() {
        return normalFragment;
    }

    public OtcAssestFragment getOtcFragment() {
        return otcFragment;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
